using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIConfirmCost : BaseNoticeEntityUI
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIConfirmCost),
            layer = EUILayer.Pop,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIConfirmCost
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject confirmPublicBinding { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textCostTitle { protected set; get; }
            public UnityEngine.GameObject costMoney { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("confirmPublicBinding", out var __tbv0);
                this.confirmPublicBinding = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textCostTitle", out var __tbv1);
                this.textCostTitle = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("costMoney", out var __tbv2);
                this.costMoney = __tbv2;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        UIN_CostMoney costMoney = null;
        UIN_Confirm confirmPublic = null;

        protected UIB_UIConfirmCost ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIConfirmCost();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.confirmPublic = this.InitChildNode<UIN_Confirm>(this.ui.confirmPublicBinding);
            this.costMoney = this.InitChildNode<UIN_CostMoney>(this.ui.costMoney);
        }

        protected override void OnShow(UIBaseData data)
        {
            //可等待模式OnShow时data为空，因为可等待模式会调用PopWindow来设值
            if (data != null)
            {
                InnerShow(data);
            }
        }

        public override async UniTask<NoticeResultType> PopWindow(UIBaseData data)
        {
            InnerShow(data);
            return await CreateAndAwaitTask();
        }

        private void InnerShow(UIBaseData data)
        {
            var uiData = data as UD_ConfirmItem;

            this.confirmPublic.Show(0, uiData.cfgNotice, uiData.finalDesc,
                uiData.callBtnConfirm, uiData.callBtnCancel, uiData.callBtnClose,
                this.OnCallClickConfirmPublic);

            this.ui.textCostTitle.text = uiData.cfgNotice.Subtext;
            this.costMoney.Show(uiData.costItemCidNums[0], uiData.costItemCidNums[1], uiData.cfgNotice.IsChangeColor);
        }

        private void OnCallClickConfirmPublic(UIN_Confirm confirm, NoticeResultType type)
        {
            CallTaskComplete(type);
        }

        protected override void OnNoticeComplete()
        {
            Close();
        }

        protected override void OnHide()
        {
            base.OnHide();
        }

        protected override void OnDispose()
        {

        }
    }
}
